package com.one.blocks.rbac.manager;

import com.alicp.jetcache.anno.CacheInvalidate;
import com.one.blocks.mvc.manager.AbstractBatchManager;
import com.one.blocks.rbac.constant.RbacConstant.RoleCache;
import com.one.blocks.rbac.domain.SysUserRole;
import com.one.blocks.rbac.mapper.SysUserRoleMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author <a href="mailto:idler41@163.con">idler41</a> created on 2022-05-01 10:06:14
 */
@Service
public class SysUserRoleManager extends AbstractBatchManager<SysUserRoleMapper, SysUserRole> {

    @Autowired
    public SysUserRoleManager(SysUserRoleMapper mapper) {
        super(mapper);
    }

    @CacheInvalidate(area = RoleCache.AREA, name = RoleCache.USER_ID, key = "#userId")
    @Transactional(rollbackFor = Exception.class)
    public boolean grant(Integer userId, List<SysUserRole> sysUserRole) {
        mapper.deleteByUserId(userId);
        if (CollectionUtils.isNotEmpty(sysUserRole)) {
            insertBatch(sysUserRole);
        }
        return true;
    }
}